<?php
/**
 * Created by PhpStorm.
 * User: dingboxu
 * Date: 17/2/21
 * Time: 下午5:07
 */

use Workerman\Protocols\Http;
use Workerman\Worker;

require_once __DIR__ . '/../../Workerman/Autoloader.php';

require_once __DIR__.'/Config/Const.php';

// 创建一个Worker监听8080端口，使用http协议通讯
// $http_worker = new \Workerman\WebServer("http://0.0.0.0:8080");

$http_worker = new Worker("http://0.0.0.0:8088");

//$http_worker->addRoot('192.168.1.113', '/var/www/html/');

// 启动4个进程对外提供服务
$http_worker->count = 4;

// 接收到浏览器发送的数据时回复hello world给浏览器
$http_worker->onMessage = function($connection,$data)
{

    $data_arr = json_decode($GLOBALS['HTTP_RAW_POST_DATA'], true);
    print_r($data_arr);
    //$data_arr = $data['post'];

    $dataDictionary = $data_arr[DATA_DICTIONARY];

    if (array_key_exists(REQUEST_ID,$data_arr)) {
        $requestId = $data_arr[REQUEST_ID];
    } else {
        $requestId = 0;
    }

    \Workerman\Protocols\Http::header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
    \Workerman\Protocols\Http::header("Access-Control-Allow-Credentials:true");

    Http::sessionStart();

    //print_r($_SESSION);
    switch($data_arr[SYS_EVENT]) {

        case EVENT_RESTAURANT_ADD:
            // 添加商铺
            new \ActionsHttp\RestaurantAdd(EVENT_RESTAURANT_ADD,$requestId,$dataDictionary,$connection);
            break;



        /*************** 商户 ***************/
        case EVENT_RESTAURANT_LOGIN:
            // 商家登录
            new \ActionsHttp\RestaurantLogin(EVENT_RESTAURANT_LOGIN,$requestId,$dataDictionary,$connection);
            break;

        case EVENT_RESTAURANT_TABLE_ADD:
            // 商家添加桌子
            new \ActionsHttp\RestaurantTableAdd(EVENT_RESTAURANT_TABLE_ADD,$requestId,$dataDictionary,$connection);
            break;


        // 商品分类
        case EVENT_RESTAURANT_FOOD_GROUPS_ADD:
            // 添加商品分类
            new \ActionsHttp\RestaurantFoodGroupsAdd(EVENT_RESTAURANT_FOOD_GROUPS_ADD,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_RESTAURANT_FOOD_GROUPS_DELETE:
            // 删除商品分类
            new \ActionsHttp\RestaurantFoodGroupsDelete(EVENT_RESTAURANT_FOOD_GROUPS_DELETE,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_RESTAURANT_FOOD_GROUPS_UPDATE:
            // 更新商品分类
            new \ActionsHttp\RestaurantFoodGroupsUpdate(EVENT_RESTAURANT_FOOD_GROUPS_UPDATE,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_RESTAURANT_FOOD_GROUPS_GET_LIST:
            // 获取商品分类列表
            new \ActionsHttp\RestaurantFoodGroupsGetList(EVENT_RESTAURANT_FOOD_GROUPS_GET_LIST,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_RESTAURANT_FOOD_GROUPS_GET_RECORD:
            // 获取单个商品分类
            new \ActionsHttp\RestaurantFoodGroupsGetRecord(EVENT_RESTAURANT_FOOD_GROUPS_GET_RECORD,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_RESTAURANT_FOOD_GROUPS_UPDATE_ORDER:
            // 更新商品分类排序
            new \ActionsHttp\RestaurantFoodGroupsUpdateOrder(EVENT_RESTAURANT_FOOD_GROUPS_UPDATE_ORDER,$requestId,$dataDictionary,$connection);
            break;

        // 商品标签
        case EVENT_RESTAURANT_FOOD_TAGS_ADD:
            // 添加商品标签
            new \ActionsHttp\RestaurantFoodTagsAdd(EVENT_RESTAURANT_FOOD_TAGS_ADD,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_RESTAURANT_FOOD_TAGS_DELETE:
            // 删除商品标签
            new \ActionsHttp\RestaurantFoodTagsDelete(EVENT_RESTAURANT_FOOD_TAGS_DELETE,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_RESTAURANT_FOOD_TAGS_UPDATE:
            // 更新商品标签
            new \ActionsHttp\RestaurantFoodTagsUpdate(EVENT_RESTAURANT_FOOD_TAGS_UPDATE,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_RESTAURANT_FOOD_TAGS_GET_LIST:
            // 获取商品标签列表
            new \ActionsHttp\RestaurantFoodTagsGetList(EVENT_RESTAURANT_FOOD_TAGS_GET_LIST,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_RESTAURANT_FOOD_TAGS_GET_RECORD:
            // 获取单个标签
            new \ActionsHttp\RestaurantFoodTagsGetRecord(EVENT_RESTAURANT_FOOD_TAGS_GET_RECORD,$requestId,$dataDictionary,$connection);
            break;

        // 商品
        case EVENT_RESTAURANT_FOOD_ADD:
            // 添加商品
            new \ActionsHttp\RestaurantFoodAdd(EVENT_RESTAURANT_FOOD_ADD,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_RESTAURANT_FOOD_DELETE:
            // 删除商品
            new \ActionsHttp\RestaurantFoodDelete(EVENT_RESTAURANT_FOOD_DELETE,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_RESTAURANT_FOOD_UPDATE:
            // 更新商品
            new \ActionsHttp\RestaurantFoodUpdate(EVENT_RESTAURANT_FOOD_UPDATE,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_RESTAURANT_FOOD_GET_LIST:
            // 获取商品列表
            new \ActionsHttp\RestaurantFoodGetList(EVENT_RESTAURANT_FOOD_GET_LIST,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_RESTAURANT_FOOD_GET_RECORD:
            // 获取单个商品信息
            new \ActionsHttp\RestaurantFoodGetRecord(EVENT_RESTAURANT_FOOD_GET_RECORD,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_RESTAURANT_FOOD_SELL_CHANGE:
            // 上架/下架商品
            new \ActionsHttp\RestaurantFoodSellChange(EVENT_RESTAURANT_FOOD_SELL_CHANGE,$requestId,$dataDictionary,$connection);
            break;

        case EVENT_TABLE_ADD:
            // 添加桌子
            new \ActionsHttp\TableAdd(EVENT_TABLE_ADD,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_TABLE_UPDATE:
            // 更新桌子
            new \ActionsHttp\TableUpdate(EVENT_TABLE_UPDATE,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_TABLE_GET_RECORD:
            // 获取单个桌子记录
            new \ActionsHttp\TableGetRecord(EVENT_TABLE_GET_RECORD,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_TABLE_GET_LIST:
            // 获取所有桌子
            new \ActionsHttp\TableGetList(EVENT_TABLE_GET_LIST,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_TABLE_DELETE:
            // 删除桌子
            new \ActionsHttp\TableDelete(EVENT_TABLE_DELETE,$requestId,$dataDictionary,$connection);
            break;


        case EVENT_RESTAURANT_FOOD_UPDATE_ORDER:
            // 更新商品排序商品
            new \ActionsHttp\RestaurantFoodUpdateOrder(EVENT_RESTAURANT_FOOD_UPDATE_ORDER,$requestId,$dataDictionary,$connection);
            break;


        /******************* 顾客 **********************/

        case EVENT_CONSUMER_LOGIN:
            // 顾客登录
            new \ActionsHttp\ConsumerLogin(EVENT_CONSUMER_LOGIN,$requestId,$dataDictionary,$connection);
            break;

        case EVENT_CONSUMER_ORDER_NEW:
            // 顾客下单
            new \ActionsHttp\ConsumerOrderNew(EVENT_CONSUMER_ORDER_NEW,$requestId,$dataDictionary,$connection);
            break;


        default:
            $connection->send(\Actions\ActionUtils::getResponseData(array(),$data_arr[SYS_EVENT],$requestId,RESULT_CODE_NOT_FOUND,"没有找到该通讯"));
            break;

    }
};

// 运行worker
Worker::runAll();